package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    // 获取所有三级分类信息 一级分类下的子菜单 二级分类下的子菜单 三级分类下的菜单
    @Override
    public List<CategoryEntity> listWithTree() {
        // 1 先获取所有的分类信息
        List<CategoryEntity> allList = this.baseMapper.selectList(null);
        // 2 获取所有的一级分类
        allList.stream().filter((level1) -> {
            return level1.getParentCid() == 0;
            // 2.1 为该一级分类设置子菜单
        }).map((menu) -> {
            menu.setChildren(this.getChildren(menu, allList));
            return menu;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        return allList;
    }

    /***
     * @Description: 递归查询所有子菜单
     * @Author: LiQianJin
     * @Date: 2024/3/24 19:01
     * @param now: 当前分类
     * @param rootList: 所有分类
     * @return: java.util.List<com.atguigu.gulimall.product.entity.CategoryEntity>
     **/
    private List<CategoryEntity> getChildren(CategoryEntity now, List<CategoryEntity> rootList) {
        List<CategoryEntity> childrenPath = rootList.stream().filter(categoryEntity -> {
            // 如果菜单父ID等于当前分类ID  那当前菜单就是子菜单
            return categoryEntity.getParentCid() == now.getCatId();
        }).map((menu) -> {
            menu.setChildren(this.getChildren(menu, rootList));
            return menu;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return childrenPath;
    }

    @Override
    public Integer batchRemove(List<Long> idList) {
        // TODO: 如果当前分类被其他地方所引用则不能进行删除
        int count = this.baseMapper.deleteBatchIds(idList);
        return count;
    }

    /**
     * @param catelogId:当前分类ID
     * @Description:
     * @Author: LiQianJin
     * @Date: 2024/3/30 16:06
     * @return: java.util.List<java.lang.Long>
     **/
    @Override
    public List<Long> findCategoryPath(Long catelogId) {
        // 定义一个容器来记录当前分类ID
        List<Long> paths = new ArrayList<>();
        // 递归查询类的完整路径
        List<Long> recursionPath = this.findRecursionPath(catelogId, paths);
        return recursionPath;
    }

    /***
     * @Description: 递归查找类的完整路径
     * @Author: LiQianJin
     * @Date: 2024/3/30 16:06
     * @param catelogId: 当前分类ID
     * @param paths: 收集分类ID容器
     * @return: java.util.List<java.lang.Long>
     **/
    private List<Long> findRecursionPath(Long catelogId, List<Long> paths) {
        paths.add(catelogId);
        CategoryEntity categoryEntity = this.getById(catelogId);
        if (categoryEntity.getParentCid() != 0) {
            this.findRecursionPath(categoryEntity.getParentCid(), paths);
        }
        return paths;
    }

}